package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.dao.IAccountDao;
import com.yuantu.judiciary.dao.IUserInfoDao;
import com.yuantu.judiciary.exception.enums.AccountExceptionEnum;
import com.yuantu.judiciary.exception.enums.UserExceptionEnum;
import com.yuantu.judiciary.model.domain.AccountDO;
import com.yuantu.judiciary.model.domain.UserInfoDO;
import com.yuantu.judiciary.model.dto.SendTimerAuthDataDTO;
import com.yuantu.judiciary.model.dto.SendTimerQueryDataDTO;
import com.yuantu.judiciary.model.enums.EndpointType;
import com.yuantu.judiciary.model.enums.SmsTemplateType;
import com.yuantu.judiciary.model.form.SendAuthorizedCodeAgainForm;
import com.yuantu.judiciary.model.form.SendAuthorizedCodeForm;
import com.yuantu.judiciary.model.form.SendCodeForm;
import com.yuantu.judiciary.model.form.VerifyCodeForm;
import com.yuantu.judiciary.model.vo.QueryResultSimpleVO;
import com.yuantu.judiciary.service.ISmsService;
import com.yuantu.judiciary.utils.AssertUtil;
import com.yuantu.judiciary.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 短信模块
 *
 * @author syw
 * @since 2023-06-08
 **/
@RestController
@RequestMapping(value = "/sms")
@Api(tags = "短信模块")
@RequiredArgsConstructor
public class SmsController extends BaseController {

    private final ISmsService smsService;
    private final IAccountDao accountDao;
    private final IUserInfoDao userInfoDao;

    @ApiOperation(value = "发送短信验证码")
    @PostMapping("/send/code")
    public Response<Boolean> sendCode(@RequestBody @Validated SendCodeForm form) {
        return ResponseFactory.okResponse(smsService.sendCode(form));
    }

    @ApiOperation(value = "验证短信验证码")
    @PostMapping("/verify/code")
    public Response<Boolean> verifyCode(@RequestBody @Validated VerifyCodeForm form) {
        return ResponseFactory.okResponse(smsService.verifyCode(form));
    }


    @ApiOperation(value = "测试发送定时添加提示短信")
    @GetMapping("/test/timing/add")
    public Response<Boolean> testTimingAdd() {
        return ResponseFactory.okResponse(smsService.sendTimerMsgCode("15951720995", new SendTimerAuthDataDTO("测试", "测试"),
                null, SmsTemplateType.TIMER_AUTH));
    }


    @ApiOperation(value = "测试发送定时案件查看提示短信")
    @GetMapping("/test/timing/query")
    public Response<Boolean> testTimingQuery() {
        String url = "https://admin.njxnet.com/cxb/#/pages/view/overview?monitorSign=XXX";
        String content = "1条民事案件";
        String name = "石逸";
        return ResponseFactory.okResponse(smsService.sendTimerMsgCode("18851450780", null,
                new SendTimerQueryDataDTO(name, content, url), SmsTemplateType.TIMER_AUTH));
    }


    @ApiOperation(value = "发送授权短信")
    @PostMapping("/send/authorized")
    public Response<Boolean> sendAuthorizedCode(@RequestBody @Validated SendAuthorizedCodeForm form) {
        Long userId = getLoginAccount().getAccountId();
        EndpointType endpointType = getLoginAccount().getEndpoint();
        return ResponseFactory.okResponse(smsService.sendAuthorizedCode(userId, endpointType, form));
    }

    @ApiOperation(value = "发送授权短信-支付宝小程序")
    @PostMapping("/send/authorized/ali")
    public Response<Boolean> sendAuthorizedCodeByAli(@RequestBody @Validated SendAuthorizedCodeForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(smsService.sendAuthorizedCodeByAli(userId, form));
    }

    @ApiOperation(value = "发送授权短信-查自己报告(支付宝)")
    @PostMapping("/send/authorized/by-self")
    public Response<Long> sendAuthorizedCodeBySelf(@RequestBody @Validated SendAuthorizedCodeForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(smsService.sendAuthorizedCodeBySelf(accountId, form));
    }


    //todo 查自己报告(微信)
    @ApiOperation(value = "发送授权短信-查自己报告(微信)")
    @GetMapping("/send/authorized/by-self/wx")
    public Response<QueryResultSimpleVO> sendAuthorizedCodeBySelfForWx(@RequestParam(required = false) String codeInfo) {
        Long accountId = getLoginAccount().getAccountId();
        AccountDO account = accountDao.findByIdAndDeletedFalse(accountId);
        UserInfoDO userInfo = userInfoDao.findByIdAndDeletedFalse(account.getUserId());
        String idNumber = userInfo.getCertNo();
        if(StringUtil.isEmpty(idNumber)){
            return ResponseFactory.errResponse(
                    "未完成实名认证,请使用【查个人】功能进行查询。");
        }

        return ResponseFactory.okResponse(smsService.sendAuthorizedCodeBySelfForWx(accountId, codeInfo));
    }


    @ApiOperation(value = "发送认证短信")
    @PostMapping("/send/auth/org")
    public Response<Boolean> sendOrgAuthCode() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(smsService.sendOrgAuthCode(accountId));
    }

    @ApiOperation(value = "发送授权成功短信")
    @PostMapping("/send/auth/success")
    public Response<Boolean> sendAuthSuccess() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(smsService.sendOrgAuthCode(accountId));
    }


    @ApiOperation(value = "重新发送授权信息-支付宝小程序")
    @PostMapping("/send/authorized/again")
    public Response<Boolean> sendAuthorizedCodeAgain(@RequestBody @Validated SendAuthorizedCodeAgainForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(smsService.sendAuthorizedCodeAgain(accountId, form));
    }

}
